{% if user %}
  {% load static from staticfiles %}
  <head>
  <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.min.js"> </script>
  <link media="print, projection, screen" type="text/css" href="{{ STATIC_URL }}themes/blue/style.css" rel="stylesheet">
  <link media="print, projection, screen" type="text/css" href="{{ STATIC_URL }}css/jq.css" rel="stylesheet">
  <script type="text/javascript" src="{{ STATIC_URL }}js/jquery.tablesorter.min.js"> </script>
  </head>
  
	<script type="text/javascript">
    $(document).ready(function()
	{
		$("#reqTable").tablesorter( {sortList: [[0,0], [1,0]]} );
	}
    );
	</script>
  {{ user.username }}'s requests <br/>
  <table id="reqTable" class="tablesorter"> 
  <thead> 
  <tr> 
    <th>Date</th> 
    {% if user_type == 2 or user_type == 0 %}
        <th>Receiver</th> 
    {% endif %}
    {% if user_type == 1 or user_type == 0 %}
        <th>Sender</th> 
    {% endif %}
    <th>Status</th> 
    <th>message</th> 
    <th>Price</th>    
    <th>Comment</th> 
    {% if not user_type == 2 %}
       <th>Asset</th> 
    {% endif %}
    <th>Action</th> 
  </tr> 
  </thead> 
  <tbody>
  {% for request in requests %}
  <tr> 
    <td>{{request.0.cretion_date}}</td> 
    {% if user_type == 2 or user_type == 0 %}
        <td>{{request.0.receiver.username}}</td> 
    {% endif %}
    {% if user_type == 1 or user_type == 0 %}
        <td>{{request.0.sender.username}}</td>  
    {% endif %}    
    <td>{{request.1}}</td> 
    <td>{{request.0.request_text}}</td> 
    <td> 
       {% if request.0.price %}
          {{request.0.price}}
       {% else %}
           {% if user_type == 1 %}
                <input id="price" name="price"/>
           {% endif %}
       {% endif %}
    </td>
    <td>
       {% if request.0.status == 6 %}
           {% if user_type == 0 or user_type == 1 %}
               {{request.0.rejection_comment}} 
           {% endif %}
       {% else %}
           {{request.0.rejection_comment}} 
       {% endif %}
    </td> 
    {% if request.0.status == 6 or request.0.status == 7%}
       {% if  user_type == 0 or user_type == 1 %}
           <td> <img src="{{ MEDIA_URL }}{{request.0.linkedPurchase.all.0.asset}}" width="150" height="150"/> </td>
       {% endif %}
    {% elif not user_type == 2 %}
        <td> </td>
    {% endif %}
    {% if user_type == 0 and request.0.status == 0 %} 
            <td> <form name="requestApproveHandler" id="requestApproveHandler" method="post" action="/member/request/approve/{{request.0.id}}/">
                <input value="approve" type="submit" id="approve" name="approve">
                   {% csrf_token %}
                </form>
                <form name="requestRejectHandler" id="requestRejectHandler" method="post" action="/member/request/rejectAdmin/{{request.0.id}}/">
                    <input id="reject_comment" name="reject_comment"/>
                    <label for="reject_comment">rejection comment</label> 
                    <input value="reject" type="submit" id="reject" name="reject">
                    {% csrf_token %}
                </form>
            </td>
    {% endif %}    
    {% if user_type == 1 %} 
       <td>
       {% if request.0.status == 1 %}
        <form name="requestAcceptHandler{{request.0.id}}" id="requestAcceptHandler{{request.0.id}}" method="post" action="/member/request/accept/{{request.0.id}}/">
               <input type="button" name="acceptSubmit" value="Accept" id="submitButton">	
               {% csrf_token %}
            </form>
            <form name="requestRejectHandler" id="requestRejectHandler" method="post" action="/member/request/rejectMerchant/{{request.0.id}}/">
               <input id="reject_comment" name="reject_comment"/>
               <label for="reject_comment">rejection comment</label/> 
               <input value="reject" type="submit" id="reject" name="reject">
               {% csrf_token %}
            </form>
      
       <script>
       $("#requestAcceptHandler{{request.0.id}}").click(function() {
	      price = $('input[id="price"]').val();
	      if (price) {
	          $("#submitButton").append('<input type="hidden" name="priceVal" value="'+ price +'" />')
	      }
	      $("#requestAcceptHandler{{request.0.id}}").submit();
	   });
       </script>
       {% endif %}
       {% if request.0.status == 5 %}
           <a href="/member/shopper/requestFulFill/{{request.0.id}}/"> fulfill request </a> <br/>
           This request wiil expire on: {{request.0.expiration_date}}
       {% endif %} 
       </td> 
    {% endif %} 
    
    
    {% if user_type == 2 and request.0.status == 2 %}
       <td> 
            <form name="requestBuy" id="requestBuy" method="post" action="/member/request/buy/{{request.0.id}}/">
               <input value="buy" type="submit" id="buy" name="buy">
               {% csrf_token %}
            </form>
       </td>
       
       {% if messages %}
           {% for message in messages %}
               {{ message }} <br/>
           {% endfor %}
       {% endif %}
    {% endif %}
    
    {% if user_type == 0 and request.0.status == 6 %}
        <td> <form name="requestApproveAssetHandler" id="requestApproveAssetHandler" method="post" action="/member/request/approveAsset/{{request.0.id}}/">
                <input value="approve asset" type="submit" id="approveAsset" name="approveAsset">
                   {% csrf_token %}
                </form>
             <form name="requestRejectAssetHandler" id="requestRejectAssetHandler" method="post" action="/member/request/rejectAsset/{{request.0.id}}/">
               <input id="reject_comment" name="reject_comment"/>
               <label for="reject_comment">rejection comment</label/> 
               <input value="reject" type="submit" id="reject" name="reject">
               {% csrf_token %}
            </form>
        </td>
    {% endif %}
    
    {% if user_type == 2 and request.0.status == 7 %}
        <td> 
           <a href="{{ MEDIA_URL }}{{request.0.linkedPurchase.all.0.asset}}"> Download </a> <br/>
           Expiration date: {{request.0.linkedPurchase.all.0.expiration_date}}
        </td>
    {% endif %}
  </tr>
  {% endfor %}
  </tbody>
  </table>

{% endif %}